<?php  
	namespace Home\Controller;
	use Think\Controller;
	//首页公告管理
	//@author  程传民
	class HomenoticeController extends AllowController{

		//首页公告管理列表
		public function index(){
			$homenotice = D("homenotice");
			$tot = $homenotice->count();
			//分页管理
			$nums = 5;
	    	$page = new \Think\Page($tot,$nums);

			$list = $homenotice->limit($page->firstRow,$page->listRows)->order("addtime desc")->select();

			// 接受当前页,默认打开为1
	    	if (empty($_GET['p'])) {
	    		$p = "1";
	    	}else{
	    		$p = $_GET['p'];
	    	}
	    	// 分配总条数
			$this->assign("total",$tot);
			// 分配进一取整后的总页数
			$this->assign("page",ceil($tot/$nums));
			// 分配当前页的页数
			$this->assign("curr",$p);

			$this->assign("list",$list);
			$this->display("Homenotice/index");
		}

		//加载添加公告内容模板
		public function add(){
			$this->display("Homenotice/add");
		}

		//添加公告
		public function insert(){
			$date['title'] = I("post.titles");
			$date['article'] = I("post.remark");
			$date['articletype'] = I("post.articletype");
			$date['addtime'] = time();
			$homenotice = D("homenotice");
			$info = $homenotice->add($date);

			if($info){
				echo "成功";
			}else{	
				echo "失败";
			}
		}

		//加载修改公告模板
		public function edit(){
			$arr = array("id"=>I("get.id"));
			$homenotice = D("homenotice");
			$list = $homenotice->where($arr)->find();
			$this->assign("list",$list);
			$this->display("Homenotice/update");
		}

		//修改公告内容
		public function update(){
			$arr = array("id"=>I("post.id"));
			$date['title'] = I("post.titles");
			$date['article'] = I("post.remark");
			$date['articletype'] = I("post.articletype");
			$date['addtime'] = time();
			$homenotice = D("homenotice");
			$info = $homenotice->where($arr)->save($date);

			if($info){
				echo "成功";
			}else{	
				echo "失败";
			}
		}

		//删除公告
		public function delete(){
			$arr = array("id"=>I("post.id"));
			$homenotice = D("homenotice");
			$info = $homenotice->where($arr)->delete();
			if($info){
				echo "成功";
			}else{	
				echo "失败";
			}
		}

	}